﻿Imports Hinshelwood.TFSStickyBuddy.Modules.Interfaces
Imports Infragistics.Windows.Ribbon
Imports Microsoft.Practices.Composite.Regions
Imports Microsoft.Practices.Unity
Imports Microsoft.Practices.Composite.Events
Imports Hinshelwood.TFSStickyBuddy.Infrastructure
Imports Hinshelwood.TFSStickyBuddy.Modules.Model
Imports Microsoft.TeamFoundation.WorkItemTracking.Client

Namespace Presenters

    Public Class TreeByPresenter
        Implements ITreeByPresenter

        Private ReadOnly m_View As ITreeByView
        Private ReadOnly m_regionManager As IRegionManager
        Private ReadOnly m_eventAggregator As IEventAggregator
        Private ReadOnly m_container As IUnityContainer
        Private ReadOnly m_ContentService As IContentService
        Private ReadOnly m_NavigationService As INavigationService

        Public ReadOnly Property View() As ITreeByView Implements ITreeByPresenter.View
            Get
                Return m_View
            End Get
        End Property

        Public Sub New(ByVal View As ITreeByView, _
                       ByVal regionManager As IRegionManager, _
                       ByVal container As IUnityContainer, _
                       ByVal eventAggregator As IEventAggregator, _
                       ByVal ContentService As IContentService, _
                       ByVal NavigationService As INavigationService)
            m_View = View
            m_regionManager = regionManager
            m_eventAggregator = eventAggregator
            m_container = container
            m_ContentService = ContentService
            m_NavigationService = NavigationService
        End Sub

        Public Sub ShowView(ByVal TreeType As Microsoft.TeamFoundation.WorkItemTracking.Client.Node.TreeType, ByVal TeamProject As Project) Implements Interfaces.ITreeByPresenter.ShowView
            Dim ViewName As String = String.Format("TreeBy{0}View", TreeType.ToString)
            '----------------------------
            If m_regionManager.Regions(RegionNames.Shell_DockManager_DocumentSplitPane_MainTabGroup).GetView(ViewName) Is Nothing Then
                ' Setup Model     
                View.Model = m_container.Resolve(Of ITreeByPresentationModel)()
                ' Add Nav
                Dim RibbonTabItem As New RibbonTabItem
                RibbonTabItem.DataContext = View.Model
                RibbonTabItem.Header = String.Format("Tree of {0}", TreeType.ToString)
                RibbonTabItem.Visibility = Visibility.Visible

                Dim RibbonName As String = String.Format("{0}{1}", RegionNames.AreaIteration_TreeTabGroup, TreeType.ToString)
                ' AreaRibbonTabItem.SetBinding(RibbonTabItem.IsVisibleProperty, "*TODO*")
                m_NavigationService.RegisterTabRegion(RibbonName, RibbonTabItem, RegionNames.AreaIteration_TreeTabGroup)
                Dim RibbonGroupDataName As String = String.Format("{0}{1}", RibbonName, "Data")
                m_NavigationService.RegisterRibbonGroup(RibbonGroupDataName, "Data", RibbonName)
                m_NavigationService.AddButtonTool("Refresh", View.Model.RefreshDataCommand, Nothing, RibbonGroupDataName)

                Dim MenuToolName As String = String.Format("{0}{1}", RibbonGroupDataName, "Queries")

                Dim x As New MenuTool
                x.Caption = "Queries"
                x.ButtonType = MenuToolButtonType.DropDown
                x.DataContext = View.Model
                x.SetBinding(MenuTool.ItemsSourceProperty, "Queries")

                m_NavigationService.RegisterMenuTool(MenuToolName, x, RibbonGroupDataName)



                ' Add Content
                m_regionManager.Regions(RegionNames.Shell_DockManager_DocumentSplitPane_MainTabGroup).Add(View, ViewName)
            End If
            '---------------------------
            m_regionManager.Regions(RegionNames.Shell_DockManager_DocumentSplitPane_MainTabGroup).Activate(View)
            View.Model.TeamProject = TeamProject
            View.Model.DisplayBy = TreeType
            View.ShowView()
        End Sub

    End Class

End Namespace